package com.atguigu.microshop.product.controller;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;
import com.atguigu.microshop.product.entity.CategoryEntity;
import com.atguigu.microshop.product.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 商品三级分类
 *
 * @author songshengang
 * @email ssg19821125@126.com
 * @date 2021-02-25 16:23:38
 */
@RestController
@RequestMapping("product/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 列表
     */
    @RequestMapping("/list")
   // @RequiresPermissions("product:category:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = categoryService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 商品三级分类tree
     */
    @RequestMapping("/list/tree")
    // @RequiresPermissions("product:category:list")
    public R listTRee(){
       //List<CategoryEntity> tree=categoryService.listWithTree();//面向对象
        List<CategoryEntity> tree=categoryService.getMenuTree();
        return R.ok().put("data", tree);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{catId}")
   // @RequiresPermissions("product:category:info")
    public R info(@PathVariable("catId") Long catId){
		CategoryEntity category = categoryService.getById(catId);
        return R.ok().put("data", category);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:category:save")
    public R save(@RequestBody CategoryEntity category){
		Boolean status =  categoryService.save(category);
        if(status==false){
            return R.error("保存失败");
        }else{
            return R.ok();
        }
    }


    /**
     * 修改
     */
    @RequestMapping("/update/sort")
    //@RequiresPermissions("product:category:update")
    public R updateSort(@RequestBody CategoryEntity[] category){
        boolean status= categoryService.updateBatchById(Arrays.asList(category));
        if(status==false){
            return R.error();
        }else{
            return R.ok();
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:category:update")
    public R update(@RequestBody CategoryEntity category){
		boolean status= categoryService.updateById(category);
        if(status==false){
            return R.error();
        }else{
            return R.ok();
        }
    }

    /**
     * 删除
     * @RequestBody:获取请求体，必须发送post请求
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:category:delete")
    public R delete(@RequestBody Long[] catIds){
        //删除前看看菜单是否被其他地方应用
		//categoryService.removeByIds(Arrays.asList(catIds));
        categoryService.removeCateByIds(Arrays.asList(catIds));
        return R.ok();
    }

}
